package com.qlk.vorder.manager.impl;

import java.util.List;

import com.qlk.common.manager.BaseManager;
import com.qlk.common.utils.page.PageUtil;
import com.qlk.vorder.domain.Task;
import com.qlk.vorder.domain.query.TaskQuery;
import com.qlk.vorder.dao.TaskDao;
import com.qlk.vorder.manager.TaskManager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * TaskManager接口的实现类
 * 
 * @author zhaoyanan
 * @version 2016-7-28 14:14:00
 * 
 */
@Component
public class TaskManagerImpl extends BaseManager implements TaskManager {

	@Autowired
	private TaskDao taskDao;

	/**
	 * {@inheritDoc}
	 */
	public boolean insert(final List<Task> taskList) {
		boolean resultFlag = false;
		if (null != taskList && taskList.size() > 0) {
			for (Task task : taskList) {
				resultFlag = taskDao.insert(task);
				if (!resultFlag) {
					throw new RuntimeException("批量新增表信息异常");
				}
			}
		}

		return resultFlag;
	}

	/**
	 * {@inheritDoc}
	 */
	public boolean insert(Task task) {
		return taskDao.insert(task);
	}

	/**
	 * {@inheritDoc}
	 */
	public boolean update(final Task task) {
		return taskDao.update(task);
	}

	/**
	 * {@inheritDoc}
	 */
	public List<Task> queryTaskList(TaskQuery queryBean) {
		return taskDao.queryTaskList(queryBean);
	}

	/**
	 * {@inheritDoc}
	 */
	public List<Task> queryTaskListWithPage(TaskQuery queryBean, PageUtil pageUtil) {
		if (null == queryBean) {
			queryBean = new TaskQuery();
		}

		// 查询总数
		int totalItem = queryTaskCount(queryBean);

		if (pageUtil == null) {
			pageUtil = new PageUtil();
		}
		pageUtil.setTotalRow(totalItem);
		pageUtil.init();

		if (totalItem > 0) {
			queryBean.setPageIndex(pageUtil.getCurPage());
			queryBean.setPageSize(pageUtil.getPageSize());
			// 调用Dao翻页方法
			return taskDao.queryTaskListWithPage(queryBean);
		}
		return null;
	}

	/**
	 * {@inheritDoc}
	 */
	public int queryTaskCount(TaskQuery queryBean) {
		return taskDao.queryTaskCount(queryBean);
	}

	/**
	 * {@inheritDoc}
	 */
	public boolean delete(Task task) {
		return taskDao.delete(task);
	}

	/**
	 * {@inheritDoc}
	 */
	public Task getTaskById(Long id) {
		return taskDao.getTaskById(id);
	}

	/**
	 * {@inheritDoc}
	 */
	public boolean delete(final Task[] tasks) {
		boolean resultFlag = false;
		if (null != tasks && tasks.length > 0) {
			for (int i = 0; i < tasks.length; i++) {
				resultFlag = delete(tasks[i]);
				if (!resultFlag) {
					throw new RuntimeException("批量删除表信息异常!");
				}
			}
		}

		return resultFlag;
	}

	/**
	 * {@inheritDoc}
	 */
	public boolean exist(Task task) {
		return taskDao.exist(task);
	}
}
